import { defined, DeveloperError } from 'cesium/Cesium';
import CesiumNavigation from './CesiumNavigation';
import '../static/navigation/navigation.css';

/**
 *入口函数
 * @param {*} viewer 场景
 * @param {*} options 初始化参数
 */
function viewerCesiumNavigationMixin(viewer, options) {
  if (!defined(viewer)) {
    throw new DeveloperError('请先初始化三维地图');
  }

  const cesiumNavigation = init(viewer, options);
  console.log('初始化三维');
  cesiumNavigation.addOnDestroyListener((function (viewer) {
    return function () {
      delete viewer.cesiumNavigation;
    };
  })(viewer));

  Object.defineProperties(viewer, {
    cesiumNavigation: {
      configurable: true,
      get() {
        return viewer.cesiumWidget.cesiumNavigation;
      },
    },
  });
}

/**
 *
 * @param {CesiumWidget} cesiumWidget 三维场景
 * @param {{}} options 导航条参数
 */
viewerCesiumNavigationMixin.mixinWidget = function (cesiumWidget, options) {
  return init.apply(undefined, arguments);
};

/**
 * @param {Viewer|CesiumWidget} viewerCesiumWidget 三维视图
 * @param {{}} options 导航参数
 */
var init = function (viewerCesiumWidget, options) {
  const cesiumNavigation = new CesiumNavigation(viewerCesiumWidget, options);
  const cesiumWidget = defined(viewerCesiumWidget.cesiumWidget) ? viewerCesiumWidget.cesiumWidget : viewerCesiumWidget;
  Object.defineProperties(cesiumWidget, {
    cesiumNavigation: {
      configurable: true,
      get() {
        return cesiumNavigation;
      },
    },
  });

  cesiumNavigation.addOnDestroyListener((function (cesiumWidget) {
    return function () {
      delete cesiumWidget.cesiumNavigation;
    };
  })(cesiumWidget));

  return cesiumNavigation;
};

export default viewerCesiumNavigationMixin;
